package com.mall.book.controller;

import com.mall.book.entity.po.*;
import com.mall.book.service.*;
import com.mall.book.utils.JwtUtil;
import com.mall.book.wrapper.ResultWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Dizziness on 2022/4/27 10:36
 */
@Controller
public class BossUserCenter {
    @Autowired
    UserInfoService userInfoService;

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    SubOrderInfoService subOrderInfoService;

    @Autowired
    OrderStatusService orderStatusService;

    @Autowired
    UserBehaviorService userBehaviorService;

    /**
     * 获取用户行为
     */
    @PostMapping("/boss/getUserBehavior")
    @ResponseBody
    public ResultWrapper getUserBehavior(HttpServletRequest request){
        try {
            JwtUtil.verifyToken(request.getHeader("Authorization"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        String user_account = request.getParameter("user_account");
        UserBehavior behavior = userBehaviorService.getById(user_account);
        return ResultWrapper.builder().code(200).msg("获取用户行为成功").data(behavior).build();
    }

    /**
     * 查询平台订单
     */
    @PostMapping("/boss/getOrders")
    @ResponseBody
    public ResultWrapper getOrders(HttpServletRequest request){
        try {
            JwtUtil.verifyToken(request.getHeader("Authorization"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String user_account = request.getParameter("user_account");

        //获取父订单信息
        List<OrderInfo> orderInfos = orderInfoService.getUserOrdersEvenDeleted(user_account);

        ArrayList<Map> result = new ArrayList<>();
        for (OrderInfo orderInfo : orderInfos) {
            HashMap<String, Object> father = new HashMap<>();
            father.put("info", orderInfo);
            Collection<SubOrderInfo> children = subOrderInfoService.getByFatherIdEvenDeleted(orderInfo.getOrderId());
            List<Map> sons = new ArrayList<>();
            for (SubOrderInfo child : children) {
                HashMap<String, Object> son = new HashMap<>();
                son.put("info", child);
                List<OrderStatus> byIdEvenDeleted = orderStatusService.getByIdEvenDeleted(child.getSubOrderId());
                son.put("states",byIdEvenDeleted);

                sons.add(son);
            }
            father.put("children", sons);
            result.add(father);
        }

        return ResultWrapper.builder().code(200).msg("获取用户订单信息成功").data(result).build();
    }

    //查询用户信息
    @PostMapping("/boss/getUserInfo")
    @ResponseBody
    public ResultWrapper getUserInfo(HttpServletRequest request){
        try {
            JwtUtil.verifyToken(request.getHeader("Authorization"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String user_account = request.getParameter("user_account");
        UserInfo userInfo = userInfoService.getByIdEvenDeleted(user_account);
        return ResultWrapper.builder().code(200).msg("获取账号基础信息成功").data(userInfo).build();
    }

    //登录授权
    @PostMapping("/boss/allowLogin")
    @Transactional
    @ResponseBody
    public ResultWrapper allowLogin(HttpServletRequest request){
        try {
            JwtUtil.verifyToken(request.getHeader("Authorization"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String deleted_user = request.getParameter("user_account");
        int i = userInfoService.allowLogin(deleted_user);
        if(i == 0) {
            throw new RuntimeException("恢复用户登录权限失败");
        }
        return ResultWrapper.builder().code(200).msg("恢复用户登录权限成功").build();
    }

    //限制登录
    @PostMapping("boss/userDelete")
    @Transactional
    @ResponseBody
    public ResultWrapper userDelete(HttpServletRequest request){
        try {
            JwtUtil.verifyToken(request.getHeader("Authorization"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String delete_user = request.getParameter("user_account");
        if (!userInfoService.removeById(delete_user)) {
            throw new RuntimeException("限制用户登录失败");
        }
        return ResultWrapper.builder().code(200).msg("限制用户登录成功").build();
    }

    //查询用户列表
    @PostMapping("/boss/getUsers")
    @ResponseBody
    public ResultWrapper getUsers(HttpServletRequest request){
        String user_account = null;
        try {
            user_account = JwtUtil.verifyToken(request.getHeader("Authorization"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        UserInfo boss = userInfoService.getById(user_account);
        if(boss == null) {
            throw new RuntimeException("当前用户不存在或权限等级太低");
        }
        List<UserInfo> users =  userInfoService.getStaffs(boss.getUserIdentity());
        return ResultWrapper.builder().code(200).msg("查询下级成功").data(users).build();
    }
}
